package com.y.architecture.location;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by youjingjing on 2017/11/7.
 */

public class LocationUtil {
    private static final String TAG = "LocationUtil";

    private static LocationUtil locationUtil;
    private static String city;
    private static String upadateTime;

    Context context;
    public LocationUtil(Context context) {
        this.context = context;
    }


    public static LocationUtil getInstance(Context context) {
        if (locationUtil == null) {
            locationUtil = new LocationUtil(context);
        }
        initLocation(context);
        return locationUtil;
    }
    //声明AMapLocationClient类对象
    private static AMapLocationClient mLocationClient;

    //声明AMapLocationClientOption对象
    private static AMapLocationClientOption mLocationOption;


    private static void initLocation(Context context) {
        //初始化定位
        mLocationClient = new AMapLocationClient(context);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
    }

    public static void configLocation() {
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    }

    public static void initLocaionListener() {
        //设置定位回调监听
        mLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation amapLocation) {
                if (amapLocation != null) {
                    if (amapLocation.getErrorCode() == 0) {
                        //可在其中解析amapLocation获取相应内容。
                        city = amapLocation.getCity();//城市信息
                        //获取定位时间
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date = new Date(amapLocation.getTime());
                        upadateTime = df.format(date);
                    } else {
                        //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                        Log.e("AmapError", "location Error, ErrCode:"
                                + amapLocation.getErrorCode() + ", errInfo:"
                                + amapLocation.getErrorInfo());
                    }
                }
            }
        });
    }

    public static void stopLocation() {
        mLocationClient.stopLocation();//停止定位后，本地定位服务并不会被销毁
        mLocationClient.onDestroy();//销毁定位客户端，同时销毁本地定位服务。
    }

    public static boolean isLocationResultEffective(AMapLocation bdLocation) {
        return bdLocation != null
                && (
//                        bdLocation.getLocationType() == AMapLocation.LOCATION_TYPE_GPS
//                || bdLocation.getLocationType() == AMapLocation.LOCATION_TYPE_CELL||
                bdLocation.getErrorCode() ==AMapLocation.LOCATION_SUCCESS);
    }

}
